﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Practices.Unity;
using Infrastucture;
using TimeSplicerModule.Factories;
using TimeSplicerModule.DomainModels;
using TimeSplicerModule.Security;
using TimeSplicerModule.ViewModels;

namespace TimeSplicerModule.Services
{
    public interface IProjectRoleService
    {
        ProjectRoleViewModel CreateNew();
        ProjectRoleViewModel Get(Guid projectRoleGuid);
        ProjectRoleViewModel Save(ProjectRoleViewModel model);
        ProjectRoleListViewModel GetListViewModel();
    }

    public class ProjectRoleService : IProjectRoleService
    {
        private readonly IUnityContainer _container;
        private ISession _session;
        private readonly IProjectRoleFactory _projectRoleFactory;
        private readonly IApplicationPermissionFactory _applicationPermissionFactory;

        public ProjectRoleService(IUnityContainer container, IProjectRoleFactory projectRoleFactory, 
            IApplicationPermissionFactory applicationPermissionFactory)
        {
            _container = container;
            _projectRoleFactory = projectRoleFactory;
            _applicationPermissionFactory = applicationPermissionFactory;
        }

        public ProjectRoleViewModel CreateNew()
        {
            _session = _container.Resolve<ISession>();

            var notAllowed = _session.ApplicationPermissions.FirstOrDefault(x => x.Permission == ApplicationPermission.Not_Allowed);
            

            var projectRole = _projectRoleFactory.CreateNew(notAllowed.ApplicationPermissionGuid,
                notAllowed.Hierarchy, notAllowed, _session.ApplicationPermissions.Where(x => x.IsProjectRolePermission).ToList());


            var model = ModelConverter.ToModel<ProjectRoleViewModel>(projectRole);


            return model;
        }

        public ProjectRoleViewModel Get(Guid projectRoleGuid)
        {
            _session = _container.Resolve<ISession>();

            var projectRole = _projectRoleFactory.Get(x => x.ProjectRoleGuid == projectRoleGuid);
            projectRole.ApplicationPermissions = _session.ApplicationPermissions.Where(x => x.IsProjectRolePermission).ToList();
            projectRole.Permission = _session.ApplicationPermissions.FirstOrDefault(x => x.ApplicationPermissionGuid == projectRole.ApplicationPermissionGuid);

            var model = ModelConverter.ToModel<ProjectRoleViewModel>(projectRole);

            return model;
        }

        public ProjectRoleViewModel Save(ProjectRoleViewModel model)
        {
            _session = _container.Resolve<ISession>();

            var role = ModelConverter.ToModel<ProjectRoleModel>(model);
            role = _projectRoleFactory.Upsert(role);

            return Get(role.ProjectRoleGuid);
        }

        public ProjectRoleListViewModel GetListViewModel()
        {
            _session = _container.Resolve<ISession>();


            var model = new ProjectRoleListViewModel();

            model.ProjectRoles = _projectRoleFactory.GetList();

            foreach (var r in model.ProjectRoles)
            {
                r.Permission = _session.ApplicationPermissions.FirstOrDefault(x => x.ApplicationPermissionGuid == r.ApplicationPermissionGuid);
            }

            return model;
        }
    }
}
